#include "login.h"

GtkWidget *window;
GtkWidget *text_id_entry;
GtkWidget *text_password_entry;
GtkWidget *label_message;


void destroy( GtkWidget *widget, gpointer data)
{
   gtk_main_quit();
}

int main(int argc, char *argv[]){
	if (!g_thread_supported())
		g_thread_init(NULL);
	gdk_threads_init();
	gtk_init(&argc, &argv);

	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(window), "calou client");
	gtk_signal_connect (GTK_OBJECT(window), "destroy",GTK_SIGNAL_FUNC (destroy), NULL);
	gtk_container_set_border_width(GTK_CONTAINER(window), 10);

	GtkWidget *table;
	table = gtk_table_new(10, 1, FALSE);
	gtk_container_add(GTK_CONTAINER(window), table);

	GtkWidget *label;
	label = gtk_label_new(NULL);
	gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1);
	/*
	PangoFontDescription *font_desc = pango_font_description_from_string ("Serif 15");
	pango_font_description_set_size (font_desc, 15 * PANGO_SCALE);
	gtk_widget_modify_font(GTK_WIDGET(label), font_desc);
	pango_font_description_free(font_desc);
	*/
	gtk_label_set_markup(GTK_LABEL(label), "<span foreground='red' underline='double' font_desc='20'>calou for linux!</span>");

	GtkWidget *vbox;
	vbox = gtk_vbox_new(FALSE, 3);
	gtk_table_attach_defaults(GTK_TABLE(table), vbox, 0, 1, 2, 3);

	GtkWidget *label_id;
	label_id = gtk_label_new("帐号:");
	gtk_misc_set_alignment(GTK_MISC(label_id), 0, 0);
	gtk_box_pack_start(GTK_BOX(vbox), label_id, FALSE,FALSE, 3);

	text_id_entry = gtk_entry_new();
	gtk_box_pack_start(GTK_BOX(vbox), text_id_entry, FALSE, FALSE, 3);
	
	GtkWidget *label_password;
	label_password = gtk_label_new("密码：");
	gtk_misc_set_alignment(GTK_MISC(label_password), 0, 0);
	gtk_box_pack_start(GTK_BOX(vbox), label_password, FALSE, FALSE, 3);

	text_password_entry = gtk_entry_new();
	gtk_entry_set_visibility(GTK_ENTRY(text_password_entry), FALSE);
	gtk_entry_set_invisible_char(GTK_ENTRY(text_password_entry), '*');
	gtk_box_pack_start(GTK_BOX(vbox), text_password_entry, FALSE, FALSE, 3);
	GtkWidget *button_login;
	GtkWidget *hbox;
	GtkWidget *label_login;
	GtkWidget *image;
	//modified image size
	GdkPixbuf *pixbuf;
	image = gtk_image_new_from_file("./image/Sign_In.png");
	pixbuf = gtk_image_get_pixbuf(GTK_IMAGE(image));
	pixbuf = gdk_pixbuf_scale_simple(pixbuf, 30, 30, GDK_INTERP_BILINEAR);
	gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixbuf);
	
	label_login = gtk_label_new("登录");
	hbox = gtk_hbox_new(FALSE, 3);
	gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 3);
	gtk_box_pack_start(GTK_BOX(hbox), label_login, FALSE, FALSE, 3);
	button_login = gtk_button_new();
	gtk_container_add(GTK_CONTAINER(button_login), hbox);

	g_signal_connect(G_OBJECT(button_login), "clicked", G_CALLBACK(login_event), NULL);

	gtk_widget_set_size_request(GTK_WIDGET(button_login), 0, 1);
	gtk_table_attach(GTK_TABLE(table), button_login, 0, 1, 4, 5, GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 65, 35);

	GtkWidget *vbox_bottom;
	vbox_bottom = gtk_vbox_new(FALSE, 3);
	label_message = gtk_label_new(NULL);
	gtk_box_pack_start(GTK_BOX(vbox_bottom), label_message, FALSE, FALSE, 3);
	gtk_table_attach_defaults(GTK_TABLE(table), vbox_bottom, 0, 1, 7, 8);
	gtk_widget_set_size_request(GTK_WIDGET(window), 250, 450);
	gtk_widget_show_all(window);
	
	gdk_threads_enter();
	gtk_main();
	gdk_threads_leave();

	return 0;
}

void login_event(GtkWidget *button, gpointer data){
	g_printf("you clicked\n");
	int ret = 0;
	if(gtk_entry_get_text_length(GTK_ENTRY(text_id_entry)) == 0 || gtk_entry_get_text_length(GTK_ENTRY(text_password_entry)) == 0){
		gtk_misc_set_alignment(GTK_MISC(label_message), 0, 0);
		gtk_label_set_markup(GTK_LABEL(label_message), "<span foreground='red' font-desc='10'>please, \ninput your id or password!</span>");
		ret = 0;
	}else{
		char *userinfo;
		userinfo = (char *)malloc(MAXUSERINFOSIZE);
		if(userinfo == NULL){
			ret = 0;
			goto err;
		}
		memset(userinfo, 0, MAXUSERINFOSIZE);
		strcpy(userinfo,"1$id:");
		strcat(userinfo, gtk_entry_get_text(GTK_ENTRY(text_id_entry)));
		strcat(userinfo,"$password:");
		strcat(userinfo, gtk_entry_get_text(GTK_ENTRY(text_password_entry)));
		
		if(socket_send_request(userinfo) == 0){
			gtk_misc_set_alignment(GTK_MISC(label_message), 0, 0);
			gtk_label_set_markup(GTK_LABEL(label_message), "<span foreground='red' font-desc='10'>id or password is wrong!\nplease, check over it!</span>");
			ret = 0;
			goto err;
		}

		if(main_window(userinfo)){
			ret = 1;
		}
	err:
		if(userinfo != NULL){
			free(userinfo);
		}
		if(ret){
			gtk_widget_hide(window);
		}
	}
}
